﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoCAD;
using easysql;
using tcad.model;

namespace tcad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            AcadApplication AcadApp;
            AcadDocument AcadDoc;
            String filePath = @"E:\ttt1.dwg";
            filePath = "E:/长江以北_01.dwg";
            filePath = "E:/test1.dwg";
           // filePath = "E:/tt11.dwg";
          //  filePath = "E:/长江以北_41.dwg";
            //'filePath = "E:/t1.dwg";
            //      filePath = "E:/ttt2.dwg";

            AcadApp = new AcadApplication();
            AcadApp.Application.Visible = true;
            AcadDoc = AcadApp.Documents.Open(filePath, null, null);
            var ms = AcadDoc.ModelSpace;
            var cc = ms.Count;

            int i = 0;
            Dictionary<String, object> dic = new Dictionary<string, object>();

            String connStr = "user id=sa;password=youotech;initial catalog=tcad;Server=.;";

            BaseDBHelper dh = DBHelperFactory.CreateSqlServerHelper(connStr);



            using(BaseDatabase db = dh.CreateDatabaseAndOpen())
            {
                db.BeginTransaction();
                db.Execute(@"
delete from tb_circle;
delete from tb_text;
delete from tb_line;
delete from tb_arc;
delete from tb_mtext;
delete from tb_point;
delete from tb_entity;
delete from tb_coordinate;
delete from tb_polyline;
");

                foreach (AcadEntity ae in ms)
                {
                    i++;
                    switch (ae.EntityName)
                    {
                 
                        case "AcDbMText":
                            //文本
                            AcadMText atext = (AcadMText)ae;
                            double[] normal = atext.Normal;
                            double[] insertionPoint = atext.InsertionPoint;
                            MTextModel tm0 = new MTextModel()
                            {
                                text = atext.TextString,
                                x = insertionPoint[0],
                                y = insertionPoint[1],
                                z = insertionPoint[2],
                                width = atext.Width,
                                height = atext.Height,
                                objectId = atext.ObjectID,
                                ownerId = atext.OwnerID
                            };
                            db.Add("tb_mtext", tm0);
                            //  Console.WriteLine(atext.TextString + "  " + insertionPoint[0] + "," + insertionPoint[1] + "," + insertionPoint[2]);
                            break;
                        case "AcDbLine":
                            AcadLine al = (AcadLine)ae;
                            double[] startPoint = al.StartPoint;
                            double[] endPoint = al.EndPoint;

                            LineModel lm = new LineModel()
                            {
                                sx = startPoint[0],
                                sy = startPoint[1],
                                sz = startPoint[2],
                                ex = endPoint[0],
                                ey = endPoint[1],
                                ez = endPoint[2],
                                objectId = al.ObjectID,
                                ownerId = al.OwnerID
                            };
                            db.Add("tb_line", lm);

                            //线
                            break;
                        case "AcDbCircle":
                            AcadCircle ac = (AcadCircle)ae;
                            double[] normal_ac = ac.Center;
                            double radius = ac.Radius;

                            CircleModel cm = new CircleModel();
                            cm.x = normal_ac[0];
                            cm.y = normal_ac[1];
                            cm.z = normal_ac[2];
                            cm.radius = radius;
                            cm.objectId = ac.ObjectID;
                            cm.ownerId = ac.OwnerID;
                            db.Add("tb_circle", cm);
                            //圆
                            break;
                    
                        case "AcDbText":
                            //文本
                            AcadText text = (AcadText)ae;
                            double[] insertionPoint0 = text.InsertionPoint;
                            //   Console.WriteLine(text.TextString + "  " + insertionPoint0[0] + "," + insertionPoint0[1] + "," + insertionPoint0[2]);

                            TextModel tm = new TextModel();

                            tm.text = text.TextString;
                            tm.x = insertionPoint0[0];
                            tm.y = insertionPoint0[1];
                            tm.z = insertionPoint0[2];
                            tm.height = text.Height;
                            tm.objectId = text.ObjectID;
                            tm.ownerId = text.OwnerID;
                            db.Add("tb_text", tm);


                            break;
                        case "AcDbArc":
                            //弧
                            AcadArc aa = (AcadArc)ae;
                            double[] center = aa.Center;

                            ArcModel am = new ArcModel()
                            {
                                x = center[0],
                                y = center[1],
                                z = center[2],
                                radius = aa.Radius,
                                startAngle = aa.StartAngle,
                                endAngle = aa.EndAngle,
                                objectId = aa.ObjectID,
                                ownerId = aa.OwnerID
                            };
                            db.Add("tb_arc", am);
                            break;
                        case "AcDbBlockReference":
                             //块参考
                            AcadBlockReference ab = (AcadBlockReference)ae;
                            var p = ab.InsertionPoint;
                            var m = new BlockReferenceModel()
                            {
                                x = p[0],
                                y = p[1],
                                z = p[2],
                                name = ab.EffectiveName,
                                objectId = ab.ObjectID,
                                ownerId = ab.OwnerID
                            };
                            db.Add("tb_blockReference", m);
                        break;
                        case "AcDbPoint":
                            //点
                            AcadPoint ap = (AcadPoint)ae;
                            double[] nor = ap.Coordinates;
                            PointModel pm = new PointModel()
                            {
                                x = nor[0],
                                y = nor[1],
                                z = nor[2],
                                objectId = ap.ObjectID,
                                ownerId = ap.OwnerID
                            };
                            db.Add("tb_point", pm);
                            break;
                        case "AcDbPolyline":
                            dynamic apl = ae;
                            double[] cs = apl.Coordinates;


                            PolylineModel pmodel = new PolylineModel();
                            pmodel.name = ae.ObjectID.ToString();
                            pmodel.objectId = ae.ObjectID;
                            pmodel.ownerId = ae.OwnerID;

                            db.Add_AutoSetId("tb_polyline", pmodel);
                            for(var j = 0; j < cs.Length; j+=2)
                            {
                                Coordinate c = new Coordinate()
                                {
                                    pid = pmodel.id,
                                    x = cs[j],
                                    y = cs[j + 1]
                                };
                                db.Add("tb_coordinate", c);
                            }


                            break;

                        case "AcDbHatch":
                            //孵化? 填充
                            AcadHatch ah = (AcadHatch)ae;
                            //ah.GradientColor1
                            HatchModel hm = new HatchModel()
                            {
                                area = ah.Area,
                                objectId = ah.ObjectID,
                                ownerId = ah.OwnerID
                            };
                            db.Add("tb_hatch", hm);
                            break;


                        default:
                            try
                            {
                                db.Add("tb_entity", new EntityModel
                                {
                                    color = (int)ae.color,
                                    entityName = ae.EntityName,
                                    entityTransparency = ae.EntityTransparency,
                                    entityType = ae.EntityType,
                                    handle = ae.Handle,
                                    HasExtensionDictionary = ae.HasExtensionDictionary,
                                    Layer = ae.Layer,
                                    Linetype = ae.Linetype,
                                    LinetypeScale = ae.LinetypeScale,
                                    Lineweight = (int)ae.Lineweight,
                                    Material = ae.Material,
                                    ObjectID = ae.ObjectID,
                                    ObjectID32 = ae.ObjectID32,
                                    ObjectName = ae.ObjectName,
                                    OwnerID = ae.OwnerID,
                                    OwnerID32 = ae.OwnerID32,
                                    PlotStyleName = ae.PlotStyleName,
                                    //     TrueColor = (int)ae.TrueColor,
                                    Visible = ae.Visible

                                });
                            }
                            catch (Exception ex)
                            {

                            }
                            break;

                    
                        //case "AcDbAttributeDefinition":
                        //    AcadAttribute aab = (AcadAttribute)ae;
                        //    break;
                        //case "AcDbSolid":
                        //    //固体?
                        //    AcadSolid aso = (AcadSolid)ae;
                        //    break;

                        //case "AcDbBlockReference":
                        //    //块参考
                        //    AcadBlockReference ab = (AcadBlockReference)ae;

                        //    break;
                        //case "AcDbRasterImage":
                        //    //光栅图像
                        //    AcadRasterImage ar = (AcadRasterImage)ae;
                        //  //  ms.AddRaster()
                        //    break;
                      


                    }

                }
                db.CommitTranscation();
           
            }

      //      AcadApp.Quit();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           



        }

        private void button2_Click(object sender, EventArgs e)
        {
            AcadApplication app = new AcadApplication();
            AcadDocument doc = app.Documents.Add("");



            String connStr = "user id=sa;password=youotech;initial catalog=tcad;Server=.;";

            BaseDBHelper dh = DBHelperFactory.CreateSqlServerHelper(connStr);

            List<TextModel> tmList = null;
            List<CircleModel> cmList = null;
            List<LineModel> lmList = null;
            List<ArcModel> arcList = null;
            List<MTextModel> mtmList = null;
            List<PointModel> pointList = null;
            List<BlockReferenceModel> brmList = null;
            List<PolylineInfo> piList = null;
            List<HatchModel> hmList = null;


            using(BaseDatabase db = dh.CreateDatabaseAndOpen())
            {
                tmList = db.QueryBySql<TextModel>("select * from tb_text");
                cmList = db.QueryBySql<CircleModel>("select * from tb_circle");
                lmList = db.QueryBySql<LineModel>("select * from tb_line");
                arcList = db.QueryBySql<ArcModel>("select * from tb_arc");
                mtmList = db.QueryBySql<MTextModel>("select * from tb_mtext");

                pointList = db.QueryBySql<PointModel>("select * from tb_point");
                brmList = db.QueryBySql<BlockReferenceModel>("select * from tb_blockReference");
                hmList = db.QueryBySql<HatchModel>("select * from tb_hatch");


                piList = db.QueryBySql<PolylineInfo>("select * from tb_polyline");
                piList.ForEach(pi =>
                {
                    var cs = db.QueryBySql<Coordinate>("select * from tb_coordinate where pid=" + pi.id);
                    pi.coordinates = cs;

                });


            }

            pointList.ForEach(m =>
            {
                doc.ModelSpace.AddPoint(new double[] {m.x,m.y,m.z });
            });

            brmList.ForEach(m =>
            {
                //doc.ModelSpace.add
            });

            piList.ForEach(m =>
            {
                double[] ds = new double[m.coordinates.Count * 3];
                for(var i = 0; i < ds.Length; i+=3)
                {
                    ds[i] = m.coordinates[i/3].x;
                    ds[i + 1] = m.coordinates[i/3].y;
                    ds[i + 2] = 0;
                }
                doc.ModelSpace.AddPolyline(ds);

            });
            hmList.ForEach(m =>
            {
                //doc.ModelSpace.AddHatch
            });





            tmList.ForEach(tm =>
            {
                double[] insertPoint = new double[] { tm.x,tm.y,tm.z};
                AcadText at = doc.ModelSpace.AddText(tm.text, insertPoint,tm.height);
                at.color = ACAD_COLOR.acRed;
            });
            mtmList.ForEach(mtm =>
            {
                double[] pp = new double[] { mtm.x,mtm.y,mtm.z};
                AcadMText mat = doc.ModelSpace.AddMText(pp, mtm.width, mtm.text);
                mat.Height = mtm.height;
                mat.color = ACAD_COLOR.acWhite;
            });


            cmList.ForEach(cm =>
            {

                double[] insertPoint = new double[] {cm.x,cm.y,cm.z };
                var ct = doc.ModelSpace.AddCircle(insertPoint, cm.radius);
                ct.color = ACAD_COLOR.acGreen;

            });

            lmList.ForEach(lm =>
            {
                double[] sp = new double[] { lm.sx,lm.sy,lm.sz};
                double[] ep = new double[] { lm.ex,lm.ey,lm.ez };
                var lt = doc.ModelSpace.AddLine(sp, ep);
                lt.color = ACAD_COLOR.acBlue;

            });

            arcList.ForEach(arc =>
            {
                double[] center = new double[] { arc.x,arc.y,arc.z };
                var at = doc.ModelSpace.AddArc(center, arc.radius, arc.startAngle, arc.endAngle);
                at.color = ACAD_COLOR.acYellow;
            });


            //doc.ModelSpace.AddArc()







        }
    }
}
